class SwitchController:
    def __init__(self, canvas, text_manager, section_manager):
        self.canvas = canvas
        self.text_manager = text_manager
        self.section_manager = section_manager
        self.switches = {}
        self.switch_states = {}  # 道岔状态：'normal' 或 'reverse'
        self._create_switches()
    
    def _create_switches(self):
        """创建道岔"""
        # 创建道岔线条
        self.switches['daocha1'] = self.canvas.create_line(140, 440, 150, 440, 300, 300, width=3)
        self.switches['daocha2_4'] = self.canvas.create_line(600, 310, 610, 310, 750, 440, 760, 440, width=3)
        self.switches['daocha6'] = self.canvas.create_line(700, 300, 850, 160, 860, 160, width=3)
        self.switches['daochaX'] = self.canvas.create_line(1100, 160, 1110, 160, 1400, 440, 1410, 440, width=3)
        
        # 创建道岔指示器
        self.switches['daocha6D'] = self.canvas.create_oval(1000, 35, 1035, 65, fill="green")
        self.switches['daocha2_4D'] = self.canvas.create_oval(1070, 35, 1105, 65, fill="green")
        
        # 初始化道岔状态
        self.switch_states['daocha6'] = 'normal'
        self.switch_states['daocha2_4'] = 'normal'
        
        # 创建道岔X标记
        self._create_switch_crosses()
        
        # 绑定道岔点击事件
        self.canvas.tag_bind(self.switches['daocha6D'], "<Button-1>", self.on_switch6_click)
        self.canvas.tag_bind(self.switches['daocha2_4D'], "<Button-1>", self.on_switch2_4_click)
    
    def _create_switch_crosses(self):
        """创建道岔X标记"""
        # 计算线段的中点坐标
        mid_x1 = (700 + 850) / 2
        mid_y1 = (300 + 160) / 2
        # 创建 "X" 形状
        self.canvas.create_line(mid_x1 - 10, mid_y1 - 10, mid_x1 + 10, mid_y1 + 10, width=2, fill="blue", tags="red_cross1")
        self.canvas.create_line(775, mid_y1 + 12, 775, mid_y1 - 12, width=2, fill="blue", tags="red_cross1")

        # 计算线段的中点坐标
        mid_x2 = (600 + 750) / 2
        mid_y2 = (310 + 440) / 2
        # 创建 "X" 形状
        self.canvas.create_line(mid_x2, mid_y2 - 12, mid_x2, mid_y2 + 12, width=2, fill="blue", tags="red_cross2")
        self.canvas.create_line(mid_x2 - 10, mid_y2 + 12, mid_x2 + 10, mid_y2 - 12, width=2, fill="blue", tags="red_cross2")
    
    def on_switch6_click(self, event):
        """道岔6点击事件"""
        # 这里可以添加道岔6的控制逻辑
        pass
    
    def on_switch2_4_click(self, event):
        """道岔2/4点击事件"""
        # 这里可以添加道岔2/4的控制逻辑
        pass
    
    def set_switch_position(self, switch_name, position):
        """设置道岔位置"""
        if switch_name in self.switch_states:
            self.switch_states[switch_name] = position
    
    def get_switch_position(self, switch_name):
        """获取道岔位置"""
        return self.switch_states.get(switch_name, 'normal')
    
    def switch_to_normal(self, switch_name):
        """道岔扳动到定位"""
        if switch_name == 'daocha6':
            self.canvas.delete(self.switches['daocha6'])
            self.switches['daocha6'] = self.canvas.create_line(700, 300, 850, 160, 860, 160, width=3)
            self.switch_states['daocha6'] = 'normal'
            self.canvas.delete("red_cross1")
        elif switch_name == 'daocha2_4':
            self.canvas.delete(self.switches['daocha2_4'])
            self.switches['daocha2_4'] = self.canvas.create_line(600, 310, 610, 310, 750, 440, 760, 440, width=3)
            self.switch_states['daocha2_4'] = 'normal'
            self.canvas.delete("red_cross2")
    
    def switch_to_reverse(self, switch_name):
        """道岔扳动到反位"""
        if switch_name == 'daocha6':
            self.canvas.delete(self.switches['daocha6'])
            self.switches['daocha6'] = self.canvas.create_line(700, 300, 860, 150, width=3, fill="black")
            self.switch_states['daocha6'] = 'reverse'
        elif switch_name == 'daocha2_4':
            self.canvas.delete(self.switches['daocha2_4'])
            self.switches['daocha2_4'] = self.canvas.create_line(610, 300, 760, 450, fill="black", width=3)
            self.switch_states['daocha2_4'] = 'reverse' 